package com.yanqu.road.dao.impl.activity.tonbing.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.tonbing.config.TonBingTitleConfig;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TonBingTitleConfigDaoImpl extends TempDao {

    private static String getTonBingTitleConfigSql = "select * from `t_s_activity_peaktongbing_title` where `activityId`=?";

    public Map<Integer, Map<Integer, TonBingTitleConfig>>  getTonBingTitleConfigMap(int activityId) {
        Map<Integer, Map<Integer, TonBingTitleConfig>>  map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getTonBingTitleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                TonBingTitleConfig data = new TonBingTitleConfig();
                data.setActivityId(rs.getInt("activityId"));
                data.setId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setIcon(rs.getInt("icon"));
                data.setName(rs.getString("name"));
                data.setEffect(rs.getInt("effect"));
                data.setRewards(rs.getString("rewards"));
                data.setCampId(rs.getInt("campId"));
                if(!map.containsKey(data.getCampId())){
                    map.put(data.getCampId(),new ConcurrentHashMap<>());
                }
                map.get(data.getCampId()).put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

}
